using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace HurricanePackage
{
    public class UGUIDragListener : MonoBehaviour,IBeginDragHandler,IDragHandler,IEndDragHandler,IMoveHandler
    {
        protected Action<PointerEventData> m_actBeginDrag;
        protected Action<PointerEventData> m_actDrag;
        protected Action<PointerEventData> m_actEndDrag;
        protected Action<AxisEventData> m_actMove;
        
        //获取是否可操作 
        protected bool Interactable
        {
            get
            {
                //判断是否可操作
                Selectable selectableMono = GetComponent<Selectable>();
                if (null != selectableMono && !selectableMono.interactable)
                    return false;

                return true;
            }
        }
        /// <summary>
        /// 获取listener
        /// </summary>
        public static UGUIDragListener Get(GameObject _go)
        {
            if (_go == null)
                return null;

            UGUIDragListener listener = _go.GetComponent<UGUIDragListener>();
            if (listener == null)
                listener = _go.AddComponent<UGUIDragListener>();
            return listener;
        }
        /// <summary>
        /// 移除listener
        /// </summary>
        public static void RemoveListener(GameObject _go)
        {
            if (_go == null)
                return;
            UGUIDragListener listener = _go.GetComponent<UGUIDragListener>();
            if(listener != null)
                Destroy(listener);
        }
        public void RemoveListener()
        {
            Destroy(this);
        }
        
        
        //注册事件
        public virtual void RegisterBeginDrag(Action<PointerEventData> _action)
        {
            if (m_actBeginDrag == null)
                m_actBeginDrag = _action;
            else
            {
                m_actBeginDrag += _action;
            }
        }
        public virtual void RegisterDrag(Action<PointerEventData> _action)
        {
            if (m_actDrag == null)
                m_actDrag = _action;
            else
            {
                m_actDrag += _action;
            }
        }
        public virtual void RegisterEndDrag(Action<PointerEventData> _action)
        {
            if (m_actEndDrag == null)
                m_actEndDrag = _action;
            else
            {
                m_actEndDrag += _action;
            }
        }
        public virtual void RegisterMove(Action<AxisEventData> _action)
        {
            if (m_actMove == null)
                m_actMove = _action;
            else
            {
                m_actMove += _action;
            }
        }
        //注销事件
        public virtual void UnRegisterBeginDrag(Action<PointerEventData> _action)
        {
            if (m_actBeginDrag == null)
                return;
            else
            {
                m_actBeginDrag -= _action;
            }
        }
        public virtual void UnRegisterDrag(Action<PointerEventData> _action)
        {
            if (m_actDrag == null)
                return;
            else
            {
                m_actDrag -= _action;
            }
        }
        public virtual void UnRegisterEndDrag(Action<PointerEventData> _action)
        {
            if (m_actEndDrag == null)
              return;
            else
            {
                m_actEndDrag -= _action;
            }
        }
        public virtual void UnRegisterMove(Action<AxisEventData> _action)
        {
            if (m_actMove == null)
              return;
            else
            {
                m_actMove -= _action;
            }
        }
        
        #region 接口实现
        public void OnBeginDrag(PointerEventData _eventData)
        {
            if (!Interactable)
                return;
            m_actBeginDrag?.Invoke(_eventData);
        }

        public void OnDrag(PointerEventData _eventData)
        {
            m_actDrag?.Invoke(_eventData);
        }

        public void OnEndDrag(PointerEventData _eventData)
        {
            if (!Interactable)
                return;
            m_actEndDrag?.Invoke(_eventData);
        }

        public void OnMove(AxisEventData _eventData)
        {
            m_actMove?.Invoke(_eventData);
        }
        #endregion

    }
}